package org.springboot.szsrcb.controller;




import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Req_team_info_log;
import org.springboot.szsrcb.entity.Test_base_info;
import org.springboot.szsrcb.entity.Test_bill;
import org.springboot.szsrcb.entity.Test_team_info_log;
import org.springboot.szsrcb.entity.Test_testing_info;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.Test_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestRestController_team {

    private static final Logger logger = LoggerFactory.getLogger(TestRestController_team.class);

	@Autowired
	private Req_bill_Service req_bill_Service;
	
	@Autowired
	private Test_bill_Service test_bill_Service;
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value = "/test_modify_detail_team_info_save")
	public boolean test_modify_detail_team_info_save(HttpServletRequest request) {
		boolean result=false;
		
		logger.info("进入 req_modify_detail_team_info_save 处理");
		
		String test_id=request.getParameter("test_id");
		String new_test_head=request.getParameter("test_head");
		//String new_req_analyst=request.getParameter("req_analyst");
		String new_test_executor=request.getParameter("test_executor");
		//Req_base_info r_base = req_bill_Service.search_req_base_info(test_id);
		Test_base_info t_base =test_bill_Service.search_a_test_base_info_by_testid(test_id);
		String old_test_head=t_base.getTest_head();
		//String old_req_analyst=t_base.getReq_analyst();
		String old_test_executor=t_base.getTest_executor();
		//req_head
		if (old_test_head==null){
			if(!new_test_head.equals("")){				
				//调入
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("user_name", new_test_head);
				map.put("operate", "调入");
				map.put("remark", "");
				map.put("role", "测试负责人");
				map.put("test_id", test_id);
				result=test_bill_Service.add_test_team_info_log(map);
			}
		}else{
			if(!new_test_head.equals(old_test_head)){
				//String[] old_head_array=old_req_head.split(",");
				//String[] new_head_array=new_req_head.split(",");
				String in_user="";
				in_user= get_in_users(new_test_head,old_test_head);
				//调入
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("user_name", in_user);
				map.put("operate", "调入");
				map.put("remark", "");
				map.put("role", "测试负责人");
				map.put("test_id", test_id);
				result=test_bill_Service.add_test_team_info_log(map);
				//调出
				String out_user="";
				out_user= get_out_users(new_test_head,old_test_head);				
				//
				Map<String, Object> map2 = new HashMap<String, Object>();
				map2.put("user_name", out_user);
				map2.put("operate", "调出");
				map2.put("remark", "");
				map2.put("role", "测试负责人");
				map2.put("test_id", test_id);
				result=test_bill_Service.add_test_team_info_log(map2);		
			}
		}
		//req_analyst
		
		//req_supervisor
		if (old_test_executor==null){
			if(!new_test_executor.equals("")){
				//调入
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("user_name", new_test_executor);
				map.put("operate", "调入");
				map.put("remark", "");
				map.put("role", "测试执行人");
				map.put("test_id", test_id);
				result=test_bill_Service.add_test_team_info_log(map);
			}
		}else{
			if(!new_test_executor.equals(old_test_executor)){
				//调入
				String in_user="";
				in_user= get_in_users(new_test_executor,old_test_executor);
				if(in_user.length()>0){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("user_name", in_user);
					map.put("operate", "调入");
					map.put("remark", "");
					map.put("role", "测试执行人");
					map.put("test_id", test_id);
					result=test_bill_Service.add_test_team_info_log(map);
				}
				//调出				
				String out_user="";
				out_user= get_out_users(new_test_executor,old_test_executor);	
				if(out_user.length()>0){
					Map<String, Object> map2 = new HashMap<String, Object>();
					map2.put("user_name", out_user);
					map2.put("operate", "调出");
					map2.put("remark", "");
					map2.put("role", "测试执行人");
					map2.put("test_id", test_id);
					result=test_bill_Service.add_test_team_info_log(map2);
				}
			}
		}
		
		//update base_info
		Map<String, Object> map_base = new HashMap<String, Object>();
		map_base.put("test_head", new_test_head);
		map_base.put("test_executor", new_test_executor);
		//map_base.put("req_analyst", new_req_analyst);
		map_base.put("test_id", test_id);
		boolean r_b=test_bill_Service.update_test_team_info(map_base);	
		result=result&&r_b;
		return result;
		
	}
	
	private String get_out_users(String new_values, String old_values) {
		String[] old_head_array=old_values.split(",");
		String[] new_head_array=new_values.split(",");
		String out_user="";
		for (int i = 0; i < old_head_array.length; i++) {
			boolean is_out=true;
			String com=old_head_array[i];
			for (int j = 0; j < new_head_array.length; j++) {
				if(com.equals(new_head_array[j])){
					is_out=false;
				}
			}					
			if(is_out){
				if(out_user.length()>1){
					out_user=out_user+","+com;
				}else{
					out_user=out_user+""+com;
				}
			}
		}
		return out_user;
	}

	private String get_in_users(String new_values, String old_values) {
		
		String[] old_head_array=old_values.split(",");
		String[] new_head_array=new_values.split(",");
		String in_user="";
		for (int i = 0; i < new_head_array.length; i++) {
			boolean is_in=true;
			String com=new_head_array[i];
			for (int j = 0; j < old_head_array.length; j++) {
				if(com.equals(old_head_array[j])){
					is_in=false;
				}
			}					
			if(is_in){
				if(in_user.length()>1){
					in_user=in_user+","+com;
				}else{
					in_user=in_user+""+com;
				}
			}
		}
		return in_user;
	}
	
	@RequestMapping(value = "/test_modify_detail_team_info_onload", method = { RequestMethod.GET })
	public String test_modify_detail_team_info_onload(HttpServletRequest request) {
		String test_id = request.getParameter("test_id");
		Test_bill test_bill=test_bill_Service.search_a_test_bill_by_id(test_id);
		Test_base_info test_base_info=test_bill_Service.search_a_test_base_info_by_testid(test_id);
		
		String return_s="";		
		
		return_s=trans_2_json_modify_test_team_info(test_bill);
		
		logger.info("retrun info :" + return_s);
		return return_s;
		
	}
	
	private String trans_2_json_modify_test_team_info(Test_bill test_bill) {
		List<User> list_u=userService.getAllUserList();
		String json_all_user=trans_all_user_2_json(list_u);
	
		String test_id=String.valueOf(test_bill.getId());
		//Req_base_info req_base_info=req_bill_Service.search_req_base_info(test_bill.getId().toString());
		Test_base_info test_base_info=test_bill_Service.search_a_test_base_info_by_testid(test_id);
		String test_head=test_base_info.getTest_head();
		String json_test_head=trans_json_for_req_head(list_u,test_head);					
		
		
		
		String json_test_executor_filter_user="";
		String json_test_executor="";
		String json_test_executor_value="";
			if(test_base_info!=null){
				json_test_executor=test_base_info.getTest_executor();
				String[] tmp1=json_test_executor.split(",");
				for (int i = 0; i < tmp1.length; i++) {
					if(i==tmp1.length){
						json_test_executor_value=json_test_executor_value+"{value : '"+tmp1[i]+"',text:'"+tmp1[i]+"'}";
					}
					else{
						json_test_executor_value=json_test_executor_value+"{value : '"+tmp1[i]+"',text:'"+tmp1[i]+"'},";
					}
				}
				json_test_executor_filter_user=filter_user_2_json(list_u,json_test_executor);
			}else{
				json_test_executor_filter_user=json_all_user;
			}		
			
		String r_start="["
						+"{type:'block',list:["
							+"{type : 'hidden',name : 'test_id',label : '需求ID',labelWidth:'70',position:'label-left', disabled:'true',value:'"+test_bill.getId()+"'}"
						+"]},"
						+"{type:'block',list:["
						 	+"{type: 'combo', label: '测试负责人', name: 'test_head', width:100,required:'true',options:["
						 				+json_test_head
						 			+"]"
							+ "},"
						/*	+ "{type: 'fieldset', label: '需求负责人',   list:["
											+"{type:'multiselect', name:'req_head', size:4, options:["
												+"{value : '"+req_base_info.getReq_head()+"',text:'"+req_base_info.getReq_head()+"'}"
											+"]},"
											+"{type : 'newcolumn'},"
											+"{type:'block',list:["
												+"{type: 'button', name: 'add1', value: '>>',width:30, offsetTop: 10},"
												+"{type: 'button', name: 'remove1', width:30,value:'<<'}"
											+"]},"
											+"{type : 'newcolumn'},"
											+"{type:'multiselect', name:'req_head_all', size:5, options:["						
												+json_all_user
											+"]}"
										+"]"
								+ "},"
								
							+"{type : 'newcolumn'},"	
							+"{type : 'input',name : 'req_supervisor',label : '需求分析人',inputWidth:'140',position:'label-left',required:'true'},"
							+"{type : 'newcolumn'},"
							+"{type: 'combo', name: 'req_head', width:100,options:["
			 							+json_req_head
			 						+"]"
			 				+ "},"*/
			 				+"{type : 'newcolumn'},"
							+ "{type: 'block', offsetLeft:10, list:["
									+"{type:'multiselect', label: '测试实施人',position: 'label-top',name:'test_executor', size:5, width:80,options:["
										+json_test_executor_value
									+"]},"
									+"{type : 'newcolumn'},"
									+"{type:'block',list:["
										+"{type: 'button', name: 'add3', value: '>>', width:30,offsetTop: 30},"
										+"{type: 'button', name: 'remove3', width:30,value:'<<'}"
									+"]},"
									+"{type : 'newcolumn'},"
									+"{type:'multiselect', name:'test_executor_all', size:5,width:80, offsetTop: 30,options:["						
										+json_test_executor_filter_user
									+"]}"
								+"]"
							+ "}"
					+"]},"
					+"{type:'block',list:["
						+"{type : 'button',name : 'team_info_save_btn',value : '保存修改'}"
					//	+"{type : 'newcolumn'},"
						//+"{type : 'button',name : 'assign_btn2',value : '退回'}"										
					+"]},"
					+"{type:'block',list:["
					+"{type : 'label',name : 'display_label',label: '项目成员变动情况'}"													
					+"]}"
				+"]";
		String r_s=r_start;
		logger.info("retrun team info :" + r_start);
		return r_s;
	}
	
	private String trans_all_user_2_json(List<User> list_u) {
		String json_s="";
		for (int i = 0; i < list_u.size(); i++) {
			User u = list_u.get(i);
			String tmp=u.getName();
			if(i==list_u.size()){
				json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'}";
			}else{
				json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'},";
			}
		}		
		
		return json_s;
	}
	
	private String trans_json_for_req_head(List<User> list_u, String test_head) {
		String json_s="{value : '',text:''},";
		if(test_head!=null){
			String tmp_head=test_head;
			String[] s_array=tmp_head.split(",");
			for (int i = 0; i < list_u.size(); i++) {
				User u = list_u.get(i);
				String tmp=u.getName();
				if(i==list_u.size()){
					for (int j = 0; j < s_array.length; j++) {
						if(tmp.equals(s_array[j])){
							json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"', selected: true}";
						}else{
							json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'}";
						}
					}
					
				}else{
					for (int j = 0; j < s_array.length; j++) {
						if(tmp.equals(tmp_head)){
							json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"', selected: true},";
						}else{
							json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'},";
						}
					}
				}
			}		
		}else{
			json_s="{value : '',text:''}";
		}
		
		return json_s;
	}
	
	private String filter_user_2_json(List<User> list_u, String json_req_analyst) {
		String json_s="";
		
		String[] s_array=json_req_analyst.split(",");
		for (int i = 0; i < list_u.size(); i++) {
			User u = list_u.get(i);
			String tmp=u.getName();
			//判断是否与旧值相同
			boolean check_r=false;
			for (int j = 0; j < s_array.length; j++) {
				if(tmp.equals(s_array[j])){	
					check_r=true;
				}
			}
			if(check_r){
				//相同不加入
			}else{
				//不相周加入
				if(i==list_u.size()){
					json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'}";
				}else{
					json_s=json_s+"{value : '"+tmp+"',text:'"+tmp+"'},";
				}
			}	
			
		}		
		
		return json_s;
	}
	
	@RequestMapping(value = "/test_modify_detail_team_info_grid", method = { RequestMethod.GET })
	public String test_modify_detail_team_info_grid(HttpServletRequest request) {
		logger.info("access /req/test_modify_detail_team_info_grid");		
		String test_id= request.getParameter("test_id");
		logger.info("test_id ="+test_id);
		Test_base_info r = test_bill_Service.search_a_test_base_info_by_testid(test_id);
		
		if(r!=null){
			//String s_tmp="req_bill_no:"+r.get(0).getReq_bill_no()+";req_title:"+r.get(0).getReq_title()+";req_type:"+r.get(0).getReq_type()+";req_dept:"+r.get(0).getReq_offer_dept()+";req_complete_date:"+r.get(0).getRequired_completion_date();
			String s_tmp=trans_2_json_req_modify_detail_team_info_grid(test_id);
			logger.info("req_modify_detail_team_info_grid :" + s_tmp);
			return s_tmp;	
		}else{
			return null;	
		}
	}	


	private String trans_2_json_req_modify_detail_team_info_grid(String test_id) {
		//List<Req_team_info_log> obj=req_bill_Service.search_req_team_info_log(r.getReq_id());
		List<Test_team_info_log> obj=test_bill_Service.search_test_team_info_log(test_id);
		String r_s="";
		if(obj!=null){
		String r_start="{rows:[";		
		String r_end="]}";
		String r_content="";
		
		for (int i = 0; i < obj.size(); i++) {
			Test_team_info_log element = obj.get(i);
			String tmp_start="{ id:"+(i+1)+",data:[";
			String tmp_end="]}";
			if((i+1)<obj.size()){
				tmp_end=tmp_end+",";
			}
			
			//String tmp_content="'0',";
			String tmp_content="";
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date_tmp= sdf.format(element.getOperate_time());			

			tmp_content=tmp_content+"'"+date_tmp+"',";
			tmp_content=tmp_content+"'"+element.getUser_name()+"',";
			tmp_content=tmp_content+"'"+element.getOperate()+"',";
			tmp_content=tmp_content+"'"+element.getRole()+"',";
			
			r_content=r_content+tmp_start+tmp_content+tmp_end;
		}
		r_s=r_start+r_content+r_end;
		logger.info("trans_2_json :" + r_s);
		}
		return r_s;
		
	}
}
	
	